<template>
	<view class="main" v-if="product">
		<!-- 主图 -->
		<view>
			<image class="pic" :src="product.itempic"></image>
		</view>
		<!-- 商品信息 -->
		<view class="info" v-if="product">
			<view class="end-price-title">券后价</view>
			<view class="num">
				<view class="price">
					<text class="now"><text class="util">￥</text>{{ product.itemendprice}}</text>
					<text class="old">￥{{product.itemprice}}</text>
				</view>
				<text class="sale">月销{{product.itemsale}}</text>
			</view>

			<text class="title double-row">{{product.itemtitle}}</text>

			<view class="coupon" v-if="product.couponMoney != 0">
				<view class="coupon-info">
					<view class="denomination">{{product.couponmoney}}元优惠券</view>
					<view class="time">使用期限 {{ getDate( product.couponstarttime )}}-
						{{ getDate( product.couponendtime)}}
					</view>
				</view>
				<view class="btn" @click="handleGoBuy">立即领券</view>
			</view>
		</view>

		<!-- 详情 -->
		<view class="product-detail" v-if="product">
			<view class="title"><text>商品详情</text></view>
			<view>
				<image class="product-image" v-for="(item, index) in banner" :src="item" :key="index" mode="widthFix">
				</image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				product: null,
				banner: []
			}
		},
		methods: {
			getHandledValue(num) {
				return num < 10 ? '0' + num : num
			},
			getDate(timeStamp, startType = 'date') {
				const d = new Date(timeStamp * 1000)
				const year = d.getFullYear()
				const month = this.getHandledValue(d.getMonth() + 1)
				const date = this.getHandledValue(d.getDate())
				const hours = this.getHandledValue(d.getHours())
				const minutes = this.getHandledValue(d.getMinutes())
				const second = this.getHandledValue(d.getSeconds())
				let resStr = ''
				if (startType === 'date') resStr = year + '-' + month + '-' + date
				else if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes +
					':' + second
				else resStr = month + '-' + date + ' ' + hours + ':' + minutes
				return resStr
			},
			handleGoBuy() {
				uni.request({
					url: "http://bbx.com/couponRatesurl",
					method: 'GET',
					data: {
						shopUrl: this.product.couponurl
					},
					success: (res) => {
						let new_model = res.data.new_model
						uni.setClipboardData({
							data: new_model,
							success() {
								uni.showToast({
									icon: 'success',
									title: '口令已复制！'
								})
							}
						})
					}
				})
			}
		},
		onLoad(options) {
			uni.request({
				url: "http://bbx.com/itemDetail",
				method: 'GET',
				data: {
					itemId: options['itemId']
				},
				success: (res) => {
					this.product = res.data.data
					this.banner = res.data.data.taobao_image.split(",")
				}
			})
		}
	}
</script>

<style lang="scss">
	.main {
		.pic {
			width: 750rpx;
			height: 750rpx;
		}

		.info {
			padding: 24rpx 32rpx;
			margin-bottom: 40rpx;
			background-color: #FFFFFF;

			.end-price-title {
				font-size: 24rpx;
				color: #FE395E;
				margin-bottom: 8rpx;
				font-weight: 600;
			}

			.num {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.price {
					display: flex;
					align-items: center;

					.now {
						color: #FE395E;
						font-weight: bold;
						font-size: 48rpx;

						.util {
							font-size: 32rpx;
						}
					}

					.old {
						text-decoration: line-through;
						font-size: 24rpx;
						color: #909399;
						margin-left: 16rpx;
					}
				}

				.sale {
					color: #909399;
					font-size: 24rpx;
					font-weight: 400;
				}
			}

			.bar {
				width: 686rpx;
				height: 84rpx;
				margin: 0 auto;
				background: url('https://qutao-app.oss-cn-shenzhen.aliyuncs.com/wxapp/20210430/5ad90aa45d3a4228a25060f305a97931.png');
				background-repeat: no-repeat;
				background-size: contain;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx;
				margin-bottom: 24rpx;

				view {
					margin-top: 16rpx;
				}

				.pay-tip {
					color: #A95700;
					font-size: 24rpx;

					text {
						color: #FE395E;
					}
				}

				.btn {
					width: 136rpx;
					height: 40rpx;
					background: linear-gradient(134deg, #92734A 0%, #C09E76 100%);
					border-radius: 4rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					text-align: center;
					line-height: 40rpx;
				}
			}

			.title {
				display: block;
				margin-bottom: 24rpx;
				font-size: 32rpx;
				color: #303133;
				line-height: 44rpx;
				position: relative;
				font-weight: 600;
			}

			.coupon {
				margin: 0 auto;
				width: 686rpx;
				height: 136rpx;
				padding: 0 26rpx;
				background: url('https://qutao-app.oss-cn-shenzhen.aliyuncs.com/wxapp/20210430/5300cae16275488da4b1eca721fb06ca.png');
				background-repeat: no-repeat;
				background-size: contain;
				display: flex;
				align-items: center;

				.coupon-info {
					color: #FFFFFF;
					margin-right: 82rpx;

					.denomination {
						font-size: 32rpx;
						font-weight: bold;
						color: #FFFFFF;
						margin-bottom: 8rpx;
					}

					.time {
						font-size: 24rpx;
						font-weight: 400;
						color: #FFFFFF;
					}
				}

				.btn {
					width: 160rpx;
					height: 56rpx;
					line-height: 56rpx;
					background: #FFFFFF;
					border-radius: 28rpx;
					text-align: center;

					font-size: 28rpx;
					font-weight: 600;
					color: #FE395E;
				}

			}

		}

		.line {
			width: 750rpx;
			height: 20rpx;
			background: #F6F7F8;
			margin-bottom: 32rpx;
		}

		.product-detail {
			.title {
				text-align: center;
				font-size: 32rpx;
				font-weight: 600;
				color: #303133;
				margin-bottom: 20rpx;

				text {
					position: relative;
				}

				text:before {
					content: "";
					width: 96rpx;
					height: 2rpx;
					background: #CCCCCC;
					position: absolute;
					left: 160rpx;
					top: 22rpx;
				}

				text:after {
					content: "";
					width: 96rpx;
					height: 2rpx;
					background: #CCCCCC;
					position: absolute;
					right: 160rpx;
					top: 22rpx;
				}
			}

			.product-image {
				width: 750rpx;
				height: auto;
			}
		}
	}
</style>
